<?php

function newPage($template) {
    return new PHPTAL('../xhtml/' . $template . '.xhtml');
}

function checkOpen() {
    $db = new PGSQLConnector('u_superuser');
    
    date_default_timezone_set('Europe/Oslo');
    
    $sql = "SELECT\n";
    $sql .= "startperiod AS start,\n";
    $sql .= "endperiod AS end\n";
    $sql .= "FROM period\n";
    $sql .= "WHERE year = '2011'";
    $rs = $db->queryUnique($sql);
    
    $start = strtotime($rs['start']);
    $end = strtotime($rs['end']);
    $now = time();
    if($start <= $now && $end >= $now) {
        return true;
    } else {
        return false;
    }
}

function date2time($day, $month, $year) {
    $date = $year . "-" . $month . "-" . $day;
    return date('Y-m-d', strtotime($date));
}

function getYear() {
    date_default_timezone_set('Europe/Oslo');
    return date('Y');
}

function getCountry() {
    return 'no';
}

function setFavorites($idoffer) {
	global $favorites;
	if(isset($_SESSION['fav'])) $favorites = $_SESSION['fav'];
	if(array_key_exists($idoffer, $favorites)) {
		//Remove offer from favorites
		
	} else {
		//Add offer to favorites
		if(count($favorites) <= 5)
			$favorites[$idoffer] = 1;
	}
	$_SESSION['fav'] = $favorites;
	print_r($favorites);
}
/*
function getFavorites() {
	global $favorites;
	$favorites = $_SESSION['fav'];
	return $favorites;
}*/

function getIdpersonFromUsername($email) {
    $db = new PGSQLConnector('u_superuser');
    
    $emailSQL = "SELECT\n";
    $emailSQL .= "un.idperson\n";
    $emailSQL .= "FROM uname AS un\n";
    $emailSQL .= "WHERE un.username = '" . $email . "'";
    $rs = $db->query2array($emailSQL);
    if(!count($rs)==0) {
        foreach($rs as $r) {
            $idperson = $r['idperson'];
        }
    }
    isset($idperson) ? $idperson : $idperson = false;
    return $idperson;
}

function getPhone($idperson) {
    $db = new PGSQLConnector('u_superuser');
    
    $phoneSQL = "SELECT\n";
    $phoneSQL .= "domesticnumber\n";
    $phoneSQL .= "FROM personphone\n";
    $phoneSQL .= "WHERE idperson = '" . $idperson . "' AND phonetypeperson = 'Private mobile' AND countrycode = 'no'";
    $phoners = $db->query2array($phoneSQL);
    if(count($phoners)>0) {
        foreach($phoners as $r) {
            $phone = '47' . $r['domesticnumber'];
        }
    }
    isset($phone) ? $phone : $phone = false;
    return $phone;
}

function setPassword($username,$phone, $sms) {
    $db = new PGSQLConnector('u_superuser');
    
    $newpass = uniqid();
    $newpassmd5 = md5($newpass);

    $insertpass = "INSERT INTO upassword (username,timecreated,password)\n";
    $insertpass .= "VALUES ('" . $username . "',now(),'" . $newpassmd5 . "')"; 

    $db->query($insertpass);

    $smsmessage = "Your username is: " . $username . " and password is: " . $newpass;
    $smsmessage .= ". Login and chenge password in Your profile. Regards, IAESTE Norway";
    $rs = $sms->sendSMS($phone,$smsmessage);
    return $rs;
}

function checkUniqUsername($username) {
    $db = new PGSQLConnector('u_superuser');
    
    $checkexistSQL = "SELECT username FROM uname WHERE username = '$username' ORDER timecreated DESC LIMIT 1";
    $checkuname = $db->query2array($checkexistSQL);
    
    if(count($checkuname) > 0) {
        $username = $username . uniqid();
        checkUnixUsername($username);
    } else return $username;
}

function setMission() {
    $m = func_get_arg(0);
    if($m==null) $_SESSION['mission'] = $mission;
    else $_SESSION['mission'] = $m;
}

function getMission() {
    return $_SESSION['mission'];
}

function getRequestString() {
    return $_SERVER['REQUEST_URI'];
}

function currencyConverter($amount,$from_currency,$to_currency = 'NOK'){
    $string = "1" . $from_currency . "=?" . $to_currency;
    //Call Google API
    $google_url = "http://www.google.com/ig/calculator?hl=en&q=".$string;
    //Get ans Store API reslts into a variable
    $result = file_get_contents($google_url);
    //Explode result to convert into an array
    $result = explode('"', $result);
    //Right side
    $converted_amount = explode(' ', $result[3]);
    $conversion = $converted_amount[0];
    $conversion = preg_replace('/[\x00-\x08\x0B-\x1F]/', '', $conversion);
    $conversion = $conversion * $amount;
    $conversion = round($conversion, 2);
    //Get text for converted currency
    $rhs_text = ucwords(str_replace($converted_amount[0], "", $result[3]));
    //Make right side string
    $rhs = $conversion.$rhs_text;
    //Left side
    $google_lhs = explode(' ', $result[1]);
    $from_amount = $google_lhs[0];
    //Get text for converted from currency
    $from_text = ucwords(str_replace($from_amount,"",$result[1]));
    //Make left hand side string
    $lhs = $amount." ".$from_text;
    //Make the result
    return $lhs." = ".$rhs;
}
?>
